<?php
class Clound_Session
{
	public $sessionProject;
    /**
     * Singleton instance
     *
     * @var Clound_Session
     */
    public static $_instance = null;
    public function __construct()
    {
        $app = Zend_Registry::get('App');
        $projectname = $app->name;
        $this->sessionProject = new Zend_Session_Namespace($projectname);

    }
    /**
     * Returns an instance of Clound_Session
     *
     * Singleton pattern implementation
     *
     * @return Clound_Session Provides a fluent interface
     */
    public static function getInstance()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }
    public function get($key){
        $out = false;
        if ($this->sessionProject->__isset($key)) {
            $out = $this->sessionProject->__get($key);
        }
    	return $out;
    }
    public function set($key,$value){
        $this->sessionProject->__set($key,$value);
    }
    /**
     * Returns array my permission
     *
     * @return Returns array my permission
     */
    public function getMyPermission(){
        $key = 'Permission';
        // Xac dinh su ton tai
        $out = $this->sessionProject->__get($key);
        if($out){
            return $out;
        }else{
            // Khoi tao
            $id = Zend_Auth::getInstance()->getIdentity()->StaffID;
            $model = new system_models_Permission();
            $out = $model->getPermission($id);
            $this->set($key,$out);
        }
        return $out;

    }
    /**
     * 
     *
     * 
     */	
    public function checkPermission($Permission){
		$arrPermission = $this->getMyPermission();
		$arrTemp = array('Roles'=>$Permission,'DefaultSet' =>'0');
		if(in_array($arrTemp, $arrPermission)){
			return true;
		}
		return false;
    }
    /**
     * Returns array staff
     *
     * @return array staff
     */
    public function getsetStaffsByPermission($sPermission,$bDefaultSet=false,$StaffIDList=''){
        // Xac dinh su ton tai
        $out = $this->sessionProject->__get($sPermission);
        if($out){
            return $out;
        }else{
            // Khoi tao
            $model = new system_models_Permission();
            $out = $model->getPermissionByCode($sPermission,$bDefaultSet,$StaffIDList);
            $this->set($sPermission,$out);
        }
        return $out;
    }
}